//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using CommunityServer.Components;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls {
    /// <summary>
    /// Summary description for SortColumn.
    /// </summary>
    public class SortableThreadColumn : Control {
        HtmlAnchor button = new HtmlAnchor();

        protected override void Render(HtmlTextWriter writer) {

            CSContext csContext = CSContext.Current;
			
			string sortImgDesc = "<img border=\"0\" src=\"" + Globals.GetSkinPath() +"/images/sort_up.gif\" />";
            string sortImg = "<img border=\"0\" src=\"" + Globals.GetSkinPath() +"/images/sort_down.gif\" />";
            string sortHref = ForumUrls.Instance().Forum(csContext.SectionID) + "&sb=" + (int) SortBy + "&d=" + (int) SortOrder.Ascending;
            string sortHrefDesc = ForumUrls.Instance().Forum(csContext.SectionID) + "&sb=" + (int) SortBy + "&d=" + (int) SortOrder.Descending;
            string href = sortHref;
			string text = Text;

            // Are we sorting by this item?
            //
            if (IsSelectedSortBy) {

                // Set the image to display
                //
                if (IsSelectedSortOrderDescending) {
                    text = Text + sortImgDesc;
                    href = sortHref;
                } else {
                    text = Text + sortImg;
                    href = sortHrefDesc;
                }

            } else {
                text = Text;
            }

            button.HRef = href;
            button.InnerHtml = text;
            button.RenderControl(writer);

        }

        public bool IsSelectedSortOrderDescending {
            get {
                int _sortOrder = CSContext.Current.GetIntFromQueryString("d",-1);

                if (_sortOrder > -1)
                    if ( ((SortOrder) _sortOrder) == SortOrder.Descending)
                        return true;

                return false;
            }
        }


        public bool IsSelectedSortBy {

            get {
                int _sortBy = CSContext.Current.GetIntFromQueryString("sb",-1);

                if (_sortBy > -1)
                    if ( ((SortThreadsBy) _sortBy) == SortBy)
                        return true;

                return false;
            }

        }

        
		[
		System.ComponentModel.DefaultValue( "" ),
		]
		public virtual String Text {
			get {
				Object state = ViewState["Text"];
				if ( state != null ) {
					return (String)state;
				}
				return "";
			}
			set {
				ViewState["Text"] = value;
			}
		}

		[
		System.ComponentModel.DefaultValue( SortThreadsBy.LastPost ),
		]
		public virtual SortThreadsBy SortBy {
			get {
				Object state = ViewState["SortBy"];
				if ( state != null ) {
					return (SortThreadsBy)state;
				}
				return SortThreadsBy.LastPost;
			}
			set {
				ViewState["SortBy"] = value;
			}
		}

		[
		System.ComponentModel.DefaultValue( SortOrder.Ascending ),
		]
		public virtual SortOrder Direction {
			get {
				Object state = ViewState["Direction"];
				if ( state != null ) {
					return (SortOrder)state;
				}
				return SortOrder.Ascending;
			}
			set {
				ViewState["Direction"] = value;
			}
		}

        private void ToggleDirection(SortOrder sortOrder) {

            if (sortOrder == SortOrder.Ascending)
                Direction = SortOrder.Descending;
            else
                Direction = SortOrder.Ascending;
        }

        public string CssClass {
            get {
                return button.Attributes["Class"];
            }
            set {
                button.Attributes["Class"] = value;
            }
        }

    }
}
